#define DEBUG
#include <cstdio>
#include <cstring>
#define MAXN 1000000
#define MAXM 1000
#define MAXK 1000

using namespace std;

int main() {
#ifdef DEBUG
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  static int group[MAXM+1][MAXK+1], link[MAXN+1][3], ind[MAXN+1];
  static bool book[MAXM+1];
  int n, k, m;
  scanf("%d %d %d", &n, &k, &m);
  for (int i=1; i<=m; i++) {
    book[i] = true;
    for (int j=1; j<=k; j++) {
      scanf("%d", &group[i][j]);
      link[i*k+j-k][0] = ind[group[i][j]];
      link[i*k+j-k][1] = i;
      link[ind[group[i][j]]][2] = i*k+j-k;
      ind[group[i][j]] = i*k+j-k;
    }
  }

  static int f[MAXN+1], que[MAXN+1];
  memset(f, 127, sizeof(f));
  f[1] = 1;
  que[1]=1;
  for (int h=0, t=1; h<t; ) {
    h++;
    for (int i=ind[que[h]]; i; i=link[i][0]) {
      int x=link[i][1];
      if (book[x]) {
	for (int j=1; j<=k; j++) {
	  if (f[group[x][j]]>f[que[h]]+1) {
	    if (link[x*k+j-k][2]) {
	      link[link[x*k+j-k][2]][0] = link[x*k+j-k][0];
	    }
	    if (link[x*k+j-k][0]) {
	      link[link[x*k+j-k][0]][2] = link[x*k+j-k][2];
	      if (!link[x*k+j-k][2]) {
		ind[group[x][j]] = link[x*k+j-k][0];
	      }
	    }
	    f[group[x][j]] = f[que[h]]+1;
	    if (group[x][j]==n) {
	      printf("%d", f[n]);
	      return 0;
	    }
	    que[++t] = group[x][j];
	  }
	}
	book[x] = false;
      }
    }
  }
  if (f[n]>n) {
    printf("-1");
  }

  fcloseall();
  return 0;
}
